package com.hrm.company.service;

import com.hrm.common.service.BaseService;
import com.hrm.common.utils.IdWorker;
import com.hrm.company.dao.DepartmentDao;
import com.hrm.domain.company.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: 林海湾Sea
 * @Date :  2021/11/22 17:33
 * @Decription:
 */
@Service
public class DepartmentService extends BaseService {

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 添加部门
     */
    public void save(Department department) {

        // 雪花算法产生的id值
        department.setId(idWorker.nextId()+"");
        department.setCreateTime(new Date());
        departmentDao.save(department);
    }

    /**
     * 更新部门信息
     */
    public void update(Department department) {
        Department sourceDepartment = departmentDao.findById(department.getId()).get();
        // 部门名
        sourceDepartment.setName(department.getName());
        // 部门父级ID
        sourceDepartment.setPid(department.getPid());
        // 部门负责人ID
        sourceDepartment.setManagerId(department.getManagerId());
        // 部门介绍
        sourceDepartment.setIntroduce(department.getIntroduce());
        // 部门负责人姓名
        sourceDepartment.setManager(department.getManager());
        // 修改部门信息后，保存修改后部门信息
        departmentDao.save(sourceDepartment);
    }

    /**
     * 根据部门id获取部门信息
     * @param id: 部门id
     * @return: 部门信息
     */
    public Department findById(String id) {
        return departmentDao.findById(id).get();
    }

    /**
     * 根据部门id删除部门
     * @param id
     */
    public void deleteDepartment(String id) {
        departmentDao.deleteById(id);
    }

    /**
     * 根据企业ID查询出--全部部门
     * @param companyId
     * @return
     */
    public List<Department> findAll(String companyId) {

        return departmentDao.findAll(getSpec(companyId));

//        List<Department> departmentList = departmentDao.findAll();
//        return departmentList;
    }

}
